package com.ef.web.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.ClassFocus;
import com.ef.domain.ClassLevel;
import com.ef.domain.ClassType;
import com.ef.service.ClassFocusService;
import com.ef.service.ClassLevelService;
import com.ef.service.ClassTypeService;
import com.ef.util.CustomExample;

public class ClassFocusAction extends BaseAction<ClassFocus> {

	private static final long serialVersionUID = 1L;

	private ClassFocusService classFocusService;

	private ClassLevelService classLevelService;
	
	private ClassTypeService classTypeService;

	private Map<String, String> classLevelList;
	
	private Map<String,String > focusTypeList;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ClassFocusAction.class);

	@SuppressWarnings("serial")
	public String list() {

		this.listResult = classFocusService.findByExample(
				new CustomExample<ClassFocus>(this.getModel()) {

					public void appendCondition(Criteria criteria) {
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {

		getListData();
		return EDIT;
	}

	private void  getListData() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		List<ClassLevel> list = classLevelService.findALLValid();
		for (ClassLevel m : list) {
			map.put(m.getId().toString(), m.getName());
		}
		classLevelList = map;
		Map<String, String> map2 = new LinkedHashMap<String, String>();
		List<ClassType> list2 = classTypeService.findALLValid();
		for (ClassType m : list2) {
			map2.put(m.getId().toString(), m.getName());
		}
		focusTypeList = map2;
	}

	public String save() {

		ClassFocus classFocus = this.getModel();
		classFocus.setUpdateTime(new Date());
		classFocus.setUpdateUser(getSessionUser());
		classFocusService.saveOrUpdate(classFocus);
		this.setResult(ConfirmMessage.ClassFocus);
		 getListData();
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			classFocusService.delete(classFocusService
					.findById(Long.valueOf(i)));
		}

		return DELETE;
	}

	public String autoCompleteAjax() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/plain;charset=GBK ");
		PrintWriter out = null;
		try {
			String q = request.getParameter("q");
			String limit = request.getParameter("limit");
			String classtypeid = request.getParameter("classtypeid");
			List<ClassFocus> list = null;
			if (classtypeid != null && classtypeid.trim().length() > 0) {
				System.out.println(classtypeid);
				list = classFocusService.findByNameAndType(q, Long
						.valueOf(classtypeid), Integer.valueOf(limit));
			} else {
				list = classFocusService.findByName(q, Integer.valueOf(limit));
			}
			// [{id:id;name:name},{id:id;name:name}]
			String responseStr = "";
			// System.out.println(responseStr);
			out = response.getWriter();
			for (ClassFocus key : list) {
				responseStr = ("{id:") + (key.getId()) + (",") + ("name:\"")
						+ (key.getName()) + ("\",type:\"")
						+ (key.getFocusType()) + ("\",level:\"")
						+ (key.getFocusLevel()) + ("\",topic:\"")
						+ (key.getDefaultTopic()) + ("\"}");
				// System.out.println(responseStr);
				out.println(responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {

				}
			}
		}
		return null;
	}

	public ClassFocusService getClassFocusService() {
		return classFocusService;
	}

	public void setClassFocusService(ClassFocusService classFocusService) {
		this.classFocusService = classFocusService;
	}

	public ClassLevelService getClassLevelService() {
		return classLevelService;
	}

	public void setClassLevelService(ClassLevelService classLevelService) {
		this.classLevelService = classLevelService;
	}

	public Map<String, String> getClassLevelList() {
		return classLevelList;
	}

	public void setClassLevelList(Map<String, String> classLevelList) {
		this.classLevelList = classLevelList;
	}

	public ClassTypeService getClassTypeService() {
		return classTypeService;
	}

	public void setClassTypeService(ClassTypeService classTypeService) {
		this.classTypeService = classTypeService;
	}

	public Map<String, String> getFocusTypeList() {
		return focusTypeList;
	}

	public void setFocusTypeList(Map<String, String> focusTypeList) {
		this.focusTypeList = focusTypeList;
	}

}
